package com.yyp.car.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yyp.car.Iservice.IStudentTTService;
import com.yyp.car.Iservice.ITeacherService;
import com.yyp.car.Iservice.IUserService;
import com.yyp.car.model.StudentTT;
import com.yyp.car.model.Teacher;
import com.yyp.car.model.User;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@Resource(name="teacherService")
	private ITeacherService teacherService;
	@Resource(name="userService")
	private IUserService userService;
	@Resource(name="studentTTService")
	private IStudentTTService istts;
	
	@RequestMapping("/addTeacher")
	public String addTeacher(Teacher teacher,User user){
		user.setTeacherMes(teacher);
		teacherService.addTeacher(teacher);
		userService.register(user);
		return "redirect:/back/teacherManager";
	}
	
	@RequestMapping("/updateTeacher")
	public String updateTeacher(Teacher teacher,User user,int uid,int tid){
		user.setId(uid);
		teacher.setId(tid);
		user.setTeacherMes(teacher);
		teacherService.updateTeacher(teacher);
		userService.update(user);
		return "redirect:/back/teacherManager";
	}
	
	@RequestMapping("/deleteTeacher/{id}/{tid}")
	public String deleteTeacher(@PathVariable int id,@PathVariable int tid){
		userService.delete(id);
		teacherService.delete(tid);
		return "redirect:/back/teacherManager";
	}
	@RequestMapping("/giveMarks")
	public String giveMarks(StudentTT stt){
		istts.add(stt);
		return "redirect:/index/cjsc";
	}
	@RequestMapping("/changeMarks")
	public String giveMarks(int sid,int ttid,int marks){
		istts.updateMarks(sid, ttid, marks);
		return "redirect:/index/cjsc";
	}
	@RequestMapping(value="/deleteMarks",method=RequestMethod.POST)
	public String deleteMarks(int sid,int ttid){
		istts.delete(istts.findId(sid, ttid));
		return "redirect:/index/cjsc";
	}
}
